<?php if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){?>
<div class="col-md-8">
<div class="main-content" id="main-content">
	<div class="sub-content">
		<div class="top-header clearfix">
			<h1>Industry</h1>
		</div>
		<div class="middle-forms user-profile1 col-md-12">
			<form id="formid" name="formid"  method="post" class="form-horizontal" role="form">
				
					<div class="form-group">
						<label class="control-label col-sm-3">Industry Name:</label>
						<div class="col-sm-9">
						<? if(isset($this->getindustrydetails) && count($this->getindustrydetails)!=0){?>
						<?foreach($this->getindustrydetails as $key=>$countriess){if($key==0){?>
						<input type="text" id="ind_name" name="ind_name" class="form-control" value="<?php if(isset($countriess->industryname)){
						echo $countriess->industryname;}?>" placeholder="Please enter industry name"/>
						<?}}}else{?>
						<input type="text" id="ind_name" name="ind_name" class="form-control" value="" placeholder="Please enter industry name"/>
						<?}?>
						</div>
					</div>
					<div class="form-group">
					<div class="col-sm-3">&nbsp;</div>
					<div class="col-sm-9">
					<?if(count($this->getallcountries)==count($this->getindustrydetails)){ $hide='style="display:none"';?>
					<input type="checkbox" id="allCountries" name="allCountries"  onclick="hideAddCountry()" checked>&nbsp;&nbsp;&nbsp;Select All Countries
					<?}else{$hide='';?>
					<input type="checkbox" id="allCountries" name="allCountries" onclick="hideAddCountry()" >&nbsp;&nbsp;&nbsp;Select All Countries
					
					<?}?></div>
					</div>
					<?if(count($this->getallcountries)==count($this->getindustrydetails) || count($this->getindustrydetails)==0){?>
					<div id="TextBoxesGroup" <?echo $hide;?>>
						<div id="TextBoxDiv0" class="form-group">
							<label class="control-label col-sm-3">Countries:</label>
							
							<div class="select_6 col-sm-9">
								<?php $cname=0;?>
								<select id="country0" name="country[]" class="form-control"> 
									<option value="0">Select</option>
										<?foreach($this->getallcountries as $countries){?>
											<option value="<? echo $countries->country_id;?>"><? echo $countries->nicename;?></option>
										<?}?>
								</select>
							</div><span id="addCountry0"><a href="javascript:void(0);" onclick="addTextBox(0)" style="display:inline-block;margin-left:10px;float:left;">Add</a></span>
						</div>
					</div>
					<?}else{?>
					<div id="TextBoxesGroup">
					<?foreach($this->getindustrydetails as $key=>$countriess){?>
						<div id="TextBoxDiv<?echo $key;?>"  class="form-group">
							<label class="control-label col-sm-3">Countries:</label>
							
							<div class="select_6 col-sm-9">
								<?php $cname=0;?>
								<select id="country<?echo $key;?>" name="country[]" class="form-control"> 
									<option value="0">Select</option>
										<? foreach($this->getallcountries as $countries){
										if($countriess->country_id==$countries->country_id){ $cname=$countries->country_id;?>
											<option value="<? echo $countries->country_id;?>" selected="selected"><? echo $countries->nicename;?></option>
										<? } else {?>
											<option value="<? echo $countries->country_id;?>"><? echo $countries->nicename;?></option>
										<?}}?>
								</select>
							</div>
							<?$k=$key+1;if($k==count($this->getindustrydetails)){?>
							<span id="addCountry<?echo $key;?>"><a href="javascript:void(0);" onclick="addTextBox(<?echo $key;?>)" style="display:inline-block;margin-left:10px;float:left;">Add</a></span>
							<?}else{?>
							<span id="addCountry<?echo $key;?>"><a href="javascript:void(0);" onclick="removeTextBox(<?echo $key;?>)" style="display:inline-block;margin-left:10px;float:left;">Delete</a></span>
							<?}?>
						</div>
						<?}?>
						</div>
						<?}?>
					<div class="clearfix"></div>
					<? if(isset($this->getindustrydetails) && count($this->getindustrydetails)!=0){?>
						<div id="categoryBoxesGroup" class="form-group"><div class="clearfix"></div>
							<?foreach($this->getindustrydetails as $key=>$countriess){if($key==0){?>
							<?$selected_values = explode(",",$countriess->category_id);
							foreach($selected_values as $cat=>$categoriess){?>
								<div id="categoryBoxDiv<?echo $cat;?>"  class="form-group">
									<label class="control-label col-sm-3">Categories:</label>							
									<div class="select_6 col-sm-9">
									<select  id="categories0" name="catgories[]" class="form-control"> 
										<option value="0">Select</option>
										<?foreach($this->getallcategories as $categories){?>
										<?if($categories->category_id==$categoriess){ ?>
											<option value="<? echo $categories->category_id;?>" selected="selected"><? echo $categories->category_name;?></option>
										<? } else {?>
											<option value="<? echo $categories->category_id;?>"><? echo $categories->category_name;?></option>
										<?}}?>
									</select>
									</div>
									<?$k=$cat+1;if($k==count($selected_values)){?>
									<span id="addCategories<?echo $cat;?>"><a href="javascript:void(0);" onclick="addCategoryBox(<?echo $cat;?>)" style="display:inline-block;margin-left:10px;float:left;">Add</a></span>
									<?}else{?>
									<span id="addCategories<?echo $cat;?>"><a href="javascript:void(0);" onclick="removeCategoryBox(<?echo $cat;?>)" style="display:inline-block;margin-left:10px;float:left;">Delete</a></span>
									<?}?>
									</div>
								<?}}}?>
								
						    </div>					
					<?}else{?>
					<div id="categoryBoxesGroup" class="form-group">
						<div id="categoryBoxDiv0">
							<label class="control-label col-sm-3">Categories:</label>							
							<div class="select_6 col-sm-9" >
							<select  id="categories0" name="catgories[]" class="form-control"> 
								<option value="0">Select</option>
								<?foreach($this->getallcategories as $categories){?>
								<option value='<?echo $categories->category_id?>'><? echo $categories->category_name?></option>;
								<?}?>
							</select>
							</div><span id="addCategories0"><a href="javascript:void(0);" onclick="addCategoryBox(0)" style="display:inline-block;margin-left:10px;float:left;">Add</a></span>
						</div>
						<div class="clearfix"></div>
					</div>
					<?}?>
					<div class="form-group">
						<div class="col-sm-3">&nbsp;</div>
						<div class="col-sm-9">
							<a href="javascript:void(0);" class="btn btn-danger" role="button" onclick="return indValidate();">Submit</a>
							
							<a class="back_btn btn btn-danger" role="button" href="<? echo $this->Url('products');?>/all-industry-list">Back</a>
						</div>
					</div>
				
				<input type="hidden" id="hid_industry_id" name="hid_industry_id" value="<?php if(isset($_GET['industry_id'])){ echo $_GET['industry_id'];} ?>">
				<input type="hidden" id="hid_indname" name="hid_check" value="<?php if(isset($this->getindustrydetails->industryname)){ echo $this->getindustrydetails->industryname;}else{echo "0";} ?>"/>
				<input type="hidden" id="hid_cname" name="hid_check" value="<?php echo $cname; ?>"/>
				<?if(count($this->getallcountries)!=count($this->getindustrydetails) || count($this->getindustrydetails)!=0){?>
				<input type="hidden" id="countryCount" name="countryCount" value="<?echo count($this->getindustrydetails)-1;?>"/>
				<?}else{?>
				<input type="hidden" id="countryCount" name="countryCount" value="0"/>
				<?}if(isset($selected_values) && count($selected_values)!=0){?>
				<input type="hidden" id="categoryCount" name="categoryCount" value="<?echo count($selected_values);?>"/>
				<?}else{?>
				<input type="hidden" id="categoryCount" name="categoryCount" value="0"/>
				<?}?>
			</form>
		</div>
		</div>
	</div>
</div>
<style>
#TextBoxesGroup label,#categoryBoxesGroup label {width: 25%; margin-bottom: 0;
    margin-top: 0;
    padding-top: 7px;color: #333;
    font-size: 16px;
    font-weight: normal;float:left;text-align:right;padding-right:15px;padding-left:15px;}
#TextBoxesGroup,#categoryBoxesGroup{overflow:hidden;}
#TextBoxesGroup .select_6 select,#categoryBoxesGroup .select_6 select{width:100%;height:34px; background-color: #fff;
    background-image: none;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
    color: #555;
    display: block;
    font-size: 14px; padding: 6px 12px;
    transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;}
	#TextBoxesGroup .select_6,#categoryBoxesGroup .select_6 {min-height: 1px;
    padding-left: 0px;
    padding-right: 0px;
    position: relative;}
</style>
<script>
	function addTextBox(count){
		var counter=count+1;
		var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv'+counter).attr("class","form-group");
		$('#addCountry'+count).html('<a href="javascript:void(0);" style="float:left;" onclick="removeTextBox('+count+')">&nbsp;&nbsp;&nbsp;Delete</a>');
		$('#countryCount').val(counter);
		$.ajax({
			type:'POST',
			url:BASE_PATH+'/products/all-countries',
			data:{counter:counter},
			success: function(data){
				newTextBoxDiv.html(data);
				newTextBoxDiv.appendTo("#TextBoxesGroup");
			}
		});
	}
	function removeTextBox(counter){
		$("#TextBoxDiv" + counter).remove();
	}
	function addCategoryBox(count){
		var counter=count+1;
		var newTextBoxDiv = $(document.createElement('div')).attr("id", 'categoryBoxDiv'+counter).attr("class","form-group");
		$('#addCategories'+count).html('<a href="javascript:void(0);" style="float:left;" onclick="removeCategoryBox('+count+')">&nbsp;&nbsp;&nbsp;Delete</a>');
		$('#categoryCount').val(counter);
		$.ajax({
			type:'POST',
			url:BASE_PATH+'/products/all-categories',
			data:{counter:counter},
			success: function(data){
				newTextBoxDiv.html(data);
				newTextBoxDiv.appendTo("#categoryBoxesGroup");
			}
		});
	}
	function removeCategoryBox(counter){
		$("#categoryBoxDiv" + counter).remove();
	}
	function hideAddCountry(){
		if(document.getElementById("allCountries").checked==true){
			$('#TextBoxesGroup').hide();
		}else{
			$('#TextBoxesGroup').show();
		}
	}
	function checkIndustryName(){
		var ind_name=$("#ind_name").val();
		$.ajax({
			type:'POST',
			url:BASE_PATH+'/products/check-industry-name',
			data:{ind_name:ind_name},
			success: function(data){
				if(data.output=='success'){	
				  $("#hid_check").val('1');	
				}else if(data.output=='notsuccess'){
				  $("#hid_check").val('0');	
				}
			}
		});
	}
	function indValidate(){
		var flag=true;
		var i=0;
		var ind_name=$("#ind_name").val();
		var countryCount=$("#countryCount").val();
		var categoryCount=$("#categoryCount").val();
		var check_ind_name=$("#hid_check").val();		
		if(ind_name==""){
			alert("Please enter industry name");
			flag=false;
		}
		if(document.getElementById("allCountries").checked==false){
			for(i=-1;i<=countryCount;i++){
				if($("#country"+(i+1)).val()==0){
					alert("Please select country name");
					flag=false;
				}
			}
		}
		
		var unique_values = {};
		var list_of_values = [];
		$('select[name$="country[]"]').
			each(function() {
			if ( ! unique_values[$(this).val()] ) {
				unique_values[$(this).val()] = true;
				list_of_values.push($(this).val());
			} else {
				alert("Country name should be unique");
				flag=false;
				return false;
			}
		});
		for(i=0;i<=categoryCount;i++){
			if($("#categories"+i).val()=="0"){
				alert("Please select category name");
				flag=false;
			}
		}
		var unique_valuesc = {};
		var list_of_valuesc = [];
		$('select[name$="catgories[]"]').
			each(function() {
			if ( ! unique_valuesc[$(this).val()] ) {
				unique_valuesc[$(this).val()] = true;
				list_of_valuesc.push($(this).val());
			} else {
				alert("Catgories name should be unique");
				flag=false;
				return false;
			}
		});
		if(flag==true){
			$("#formid").submit();
		}
	}		
</script>
<?php }else{?>
<script>
	window.location='<? echo $this->Url('home')?>';
</script>
<?php }?>
